package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.AdminOrderMapper;
import com.sky.mapper.OrderDetailMapper;

import com.sky.mapper.OrdersMapper;
import com.sky.result.PageResult;
import com.sky.service.AdminOrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminOrderServiceImpl implements AdminOrderService {
    @Autowired
    AdminOrderMapper adminOrderMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    OrdersMapper ordersMapper;


    @Override
    public OrderStatisticsVO countAmountOfOrders() {

        Integer toBeConfirmed = adminOrderMapper.countOrdersByStatus(Orders.TO_BE_CONFIRMED);

        // 待派送数量
        Integer confirmed = adminOrderMapper.countOrdersByStatus(Orders.CONFIRMED);

        // 派送中数量
        Integer deliveryInProgress = adminOrderMapper.countOrdersByStatus(Orders.DELIVERY_IN_PROGRESS);
        return OrderStatisticsVO.builder()
                .toBeConfirmed(toBeConfirmed)
                .confirmed(confirmed)
                .deliveryInProgress(deliveryInProgress)
                .build();
    }

    /**
     * 客户端取消订单
     *
     * @param ordersCancelDTO
     */

    @Override
    public void cancelOrders(OrdersCancelDTO ordersCancelDTO) {
        Orders orders = ordersMapper.findOrderById(ordersCancelDTO.getId());
        if (orders.getStatus() == Orders.TO_BE_CONFIRMED || orders.getStatus() == Orders.CONFIRMED || orders.getStatus() == Orders.DELIVERY_IN_PROGRESS
                || orders.getStatus() == Orders.COMPLETED){
            throw new OrderBusinessException("当前订单已不能取消");
        }
            BeanUtils.copyProperties(ordersCancelDTO, orders);
        if (orders.getStatus() != Orders.TO_BE_CONFIRMED && orders.getStatus() != Orders.CANCELLED) {
            orders.setStatus(Orders.CANCELLED);
            ordersMapper.updateById(orders);
        }
    }


}
